#!/usr/bin/perl -w
use strict;

# Merge 2 files with mcc_mnc.com taking precidence.
#
# from wikipedia
open(COMP_FD, 'wikipedia.com.tsv') or die;
# from mcc_mnc.com
open(REFERENCE_FD, 'mcc_mnc.com.tsv') or die;
open(MAP_FD, '>', 'merged.tsv') or die;
open(BAD_FD, '>', 'merged_bad.txt') or die;
my %compH;

while (my $comp_line = <COMP_FD>) {
    chomp $comp_line;
    my @oa = split(/\t/, $comp_line);
    $compH{"$oa[0]\t$oa[1]"} = { OPERATOR_NAME => $oa[2],
				COUNTRY_NAME => $oa[3] };

}
print "size of compH: " . (scalar keys %compH) . "\n";

my $new_records = 0;
my $updated_records_operator_name = 0;
my $updated_records_country_name = 0;
my $bad_records = 0;

while (my $ref_line = <REFERENCE_FD>) {
    chomp $ref_line;
    my @ra = split(/\t/, $ref_line);
    my $ref_key = "$ra[0]\t$ra[1]";
    my $operator_name = $ra[2];
    my $country_name = $ra[3];

    unless (defined $ra[0] and $ra[0] ne "") {
	print 'reference mcc: [' . $ra[0] . '] or mnc: [' . $ra[1] . "] was null or empty so will not use.\n";
	next;
    }

    my $lookup = $compH{$ref_key};
    if (defined $lookup) {
	# call out diff operator name or country.
	if ($lookup->{OPERATOR_NAME} ne $operator_name and $operator_name ne "") {
	    $compH{$ref_key}->{OPERATOR_NAME} = $operator_name;
	    $updated_records_operator_name++;
	}
	if ($lookup->{COUNTRY_NAME} ne $country_name and $country_name ne "ZZ") {
	    $compH{$ref_key}->{COUNTRY_NAME} = $country_name;
	    $updated_records_country_name;
	}

    } else {
	# add entry

	$compH{$ref_key} = {OPERATOR_NAME => $operator_name,
			   COUNTRY_NAME => $country_name};
	print "adding new: $ref_key\t$operator_name\t$country_name\n";
	$new_records++;
    }
}

my @newer_oid = sort { $a cmp $b } keys %compH;

foreach my $nrkey (@newer_oid) {
    my $nr = $compH{$nrkey};
    my @mccmnc = split(/\t/, $nrkey);
    my $mcc = (defined $mccmnc[0] ? ($mccmnc[0] eq "" ? 'empty' : $mccmnc[0]) : 'null');
    my $mnc = (defined $mccmnc[1] ? ($mccmnc[1] eq "" ? 'empty' : $mccmnc[1]) : 'null');

    my $operator_name = (defined $nr->{OPERATOR_NAME} ? ($nr->{OPERATOR_NAME} eq "" ? 'empty' : $nr->{OPERATOR_NAME}) : 'null');
    my $country_name = (defined $nr->{COUNTRY_NAME} ? ($nr->{COUNTRY_NAME} eq "" ? 'empty' : $nr->{COUNTRY_NAME}) : 'null');

    if ($mcc eq 'null' or $mnc eq 'null' or $operator_name eq 'null' or $mcc eq 'empty' or $mnc eq 'empty' or $operator_name eq 'empty') {
	$bad_records++;
	print BAD_FD "$nrkey\t$operator_name\t$country_name\n";
    }
# 	print "bad record:\n" .
# 	    "-----------\n" .
# 	    "mcc: $mcc\n" .
# 	    "mnc: $mnc\n" .
# 	    "operator_name: $operator_name\n" .
# 	    "country_name: $country_name\n\n";
#     }

    print MAP_FD "$mcc\t$mnc\t$operator_name\t$country_name\n";
}

close COMP_FD;
close REFERENCE_FD;
close MAP_FD;
close BAD_FD;

print "Stats\n";
print "-----\n";
print 'Size of comp database: ' .  ($#newer_oid + 1) . "\n";
print "new records: $new_records\n";
print "updated records operator: $updated_records_operator_name\n";
print "updated records country_name: $updated_records_country_name\n";

print "sorting ...\n";
`sort -k1n -k2n merged.tsv > merged_sorted.tsv`;
print "output is in merged_sorted.tsv\n";

print "\nBad records that need fixing in table: $bad_records\n";
